package vn.team.clipxonxao.ui;

import java.util.List;

import vn.team.clipxonxao.model.Category;
import vn.team.clipxonxao.model.CategoryList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.mk.fw.ui.MKFragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class AppMain extends SlidingFragmentActivity {

	private MKFragment mContent;
	public static ImageButton menuButton;
	private static TextView txtActionBarTitle;
	private CategoryList categoryItems;
	private List<Category> listCategory;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_main);
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("dataCategory");
		categoryItems = (CategoryList) bundle.getSerializable("listCategory");
		listCategory = categoryItems.getListCategory();
		if (savedInstanceState != null){
			mContent = (MKFragment) getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		}
		if (mContent == null){
			mContent = new HomeClipXonXao(listCategory.get(0).getId(),listCategory.get(0).getName());
		}
		// set the Above View
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.layout_main_content_home, mContent)
		.commit();
		
		// set the Behind View
		setBehindContentView(R.layout.layout_slidingmenu);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.layout_slidingmenu_content, new SlidingMenuFragment(listCategory))
		.commit();
		
		// customize the SlidingMenu
		SlidingMenu slideMenu = getSlidingMenu();
		slideMenu.setBehindOffset(60);
		slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		
		ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        
        View view = getLayoutInflater().inflate(R.layout.layout_custom_actionbar, null);
		menuButton = (ImageButton)view.findViewById(R.id.imgCustomActionBar);
		menuButton.setOnClickListener(new OnClickListener()
		{
		        @Override
		        public void onClick(View v)
		        {
		        	toggle();
		        }
		});
		
		txtActionBarTitle = (TextView)view.findViewById(R.id.txtActionBarTitle);
		setTitleName(getResources().getString(R.string.app_name));
		actionBar.setCustomView(view);
	}
	
	
	public static void setTitleName(String titleName){
		txtActionBarTitle.setText(titleName);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	
	
	
}
